package com.aosan.website.common.core.utils

import javax.servlet.http.HttpServletRequest

class HttpUtil {
    companion object {
        /**
         * 获取IP地址
         */
        fun ip(request: HttpServletRequest): String {
            var ip = request.getHeader("x-forwarded-for")
            if (ip != null && ip.isNotEmpty() && !"unknown".equals(ip, true)) {
                if (ip.indexOf(",") != -1) {
                    ip = ip.split(",")[0]
                }
            }
            if (ip == null || ip.isEmpty() || "unknown".equals(ip, true)) {
                ip = request.getHeader("Proxy-Client-IP")
            }
            if (ip == null || ip.isEmpty() || "unknown".equals(ip, true)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.isEmpty() || "unknown".equals(ip, true)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.isEmpty() || "unknown".equals(ip, true)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.isEmpty() || "unknown".equals(ip, true)) {
                ip = request.getHeader("X-Real-IP");
            }
            if (ip == null || ip.isEmpty() || "unknown".equals(ip, true)) {
                ip = request.remoteHost
            }
            return ip
        }
    }
}